#include "target.h"
#include "util_buffer_circular_queue.h"

void util_buffer_circular_queue_init(util_buffer_CircularQueue *buffer, U32 length, U8 *data)
{
	buffer->length = length;
	buffer->data = data;
	util_buffe_circular_queuer_clear(buffer);
}

void util_buffe_circular_queuer_clear(util_buffer_CircularQueue *buffer)
{
	buffer->writingPointer = 0;
	buffer->readingPointer = 0;
	buffer->size = 0;
}

BOOL util_buffer_circular_queue_insert(util_buffer_CircularQueue *buffer, U8 *bytes, U32 size)
{
	if(buffer->size + size <= buffer->length)
	{
		U32 i;
		
		for(i = 0; i < size; i++)
		{
			buffer->data[buffer->writingPointer++] = bytes[i];
	
			if(buffer->writingPointer == buffer->length){
				buffer->writingPointer = 0;
			}
	
			buffer->size++;
		}

		return TRUE;
	}
	else{
		return FALSE;
	}
}

BOOL util_buffer_circular_queue_remove(util_buffer_CircularQueue *buffer, U8 *bytes, U32 size)
{
	if(buffer->size >= size)
	{
		U32 i;
		
		for(i = 0; i < size; i++)
		{
			bytes[i] = buffer->data[buffer->readingPointer];
	
			buffer->readingPointer++;
	
			if(buffer->readingPointer == buffer->length)
			{
				buffer->readingPointer = 0;
			}
	
			buffer->size--;
		}

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
